<template>
  <div style="margin: 0 10px;">
    <div style="float:right;margin:5px 0px 5px 5px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="canSave"
        >新增</el-button>
        <el-button type="success" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
      </el-col>
    </div>

    <el-table :data="fileblobList" border :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" width="60px" prop="rowNumber" />
      <el-table-column label="链接名称" header-align="center" align="left" prop="ctName" />

      <el-table-column label="链接路径" header-align="center" align="left" prop="ctFile">
        <template slot-scope="scope">
          <a :href="scope.row.ctFile" target="view_window" class="ctFile-a">{{scope.row.ctFile}}</a>
        </template>
      </el-table-column>
      <el-table-column
        label="链接类型"
        header-align="center"
        align="center"
        width="100"
        prop="ctFileType"
      />

      <el-table-column label="上传日期" header-align="center" align="center" prop="dtDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dtDate, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
      >
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            title="修改/浏览"
          ></el-button>
          <el-button
            size="medium"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            title="删除"
            v-if="canSave"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-row class="mb8">
        <div style="float:right;">
          <el-button type="primary" size="mini" @click="submitForm" v-if="canSave">保 存</el-button>
        </div>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="main-form">
        <el-form-item label="上传日期" prop="dtDate" :disabled="true">
          <el-date-picker
            clearable
            size="small"
            style="width:100%;"
            v-model="form.dtDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上传日期"
            :disabled="true"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="链接名称" prop="ctName">
          <el-input v-model="form.ctName" placeholder="链接名称" :readonly="!canSave"></el-input>
        </el-form-item>

        <el-form-item label="连接地址" prop="ctFile">
          <el-input v-model="form.ctFile" placeholder="链接地址" :readonly="!canSave" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  addFileblob,
  getFileblob,
  updateFileblob,
  delFileblob,
  listFileblob,
} from '@/api/fileList/fileList'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'
import { parseTime } from '@/utils/ruoyi'

export default {
  name: 'UrlList',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 业务附件表格数据
      fileblobList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ctIdP: '999',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ctName: [
          {
            required: true,
            message: '链接名称不能为空',
            trigger: 'blur',
          },
        ],
        ctFile: [
          {
            required: true,
            message: '链接地址不能为空',
            trigger: 'blur',
          },
          {
            pattern: /(http|https):\/\/([\w.]+\/?)\S*/,
            message: '请输入以http://或https://开头的网址',
          },
        ],
        dtDate: [
          {
            required: true,
            message: '上传日期不能为空',
            trigger: 'blur',
          },
        ],
      },
      // 可否保存和表单控件disable标识
      canSave: false,
      funNo: getFunNo(this.$route),
      singleStore: null,
      subSingleStore: null,
    }
  },

  methods: {
    refresh() {
      this.masterRefresh()
    },
    /** 查询附件列表 */
    getList() {
      this.loading = true

      if (!this.queryParams.ctIdP) {
        this.singleStore = store.getters.singleStore['main' + this.funNo]
        this.subSingleStore =
          this.singleStore.subSingleStore['sub' + this.subFunNo]
        if (this.subSingleStore)
          this.queryParams.ctIdP = this.subSingleStore.ctId
      }
      //如果获取不到，则不去提取数据
      if (this.queryParams.ctIdP) {
        this.queryParams.ctTypeList = ['03']
        listFileblob(this.queryParams)
          .then((response) => {
            this.fileblobList = response.rows
            this.total = response.total
            this.loading = false
          })
          .catch(() => {
            this.loading = false
          })
      }
    },
    /** 构建字典相关*/
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        ctId: null,
        ctIdP: null,
        ctFunNo: null,
        ctType: null,
        ctName: null,
        bBlob: null,
        ctFileType: null,
        ctFile: null,
        amSize: null,
        dtDate: null,
        ctState: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.formInit()
      if (!this.form.ctIdP) {
        this.msgSuccess('主单据为空')
        return
      }
      this.open = true
      this.title = '添加链接'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const ctId = row.ctId
      getFileblob(ctId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改链接'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.ctId != null) {
            updateFileblob(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFileblob(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ctIds = row.ctId
      this.$confirm('是否确认删除附件编号为"' + ctIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delFileblob(ctIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    // 表单数据信息初始化
    formInit() {
      this.form = {}
      this.form.ctId = undefined
      this.form.ctFunNo = this.subFunNo
      this.form.funNo = this.subFunNo
      this.form.dtDate = parseTime(new Date())
      //重新获取一下ctidp
      this.singleStore = store.getters.singleStore['main' + this.funNo]
      this.subSingleStore =
        this.singleStore.subSingleStore['sub' + this.subFunNo]
      if (this.subSingleStore) this.queryParams.ctIdP = this.subSingleStore.ctId

      this.form.ctIdP = this.queryParams.ctIdP
      this.form.amSize = '0'
      this.form.ctFileType = '.url'
      this.form.ctType = '03'
    },
  },
}
</script>
<style>
.main-form .vue-treeselect__control {
  width: 100%;
}
.ctFile-a {
  text-decoration: underline;
  color: #0000dd;
}
</style>
